import pymysql
import json
import time
import os

MYSQL_HOST = 'localhost'
MYSQL_USER = 'root'
MYSQL_PASSWORD = '123456'
MYSQL_PORT = 3306
MYSQL_DB = 'mooc_source'
MYSQL_CHARSET = 'utf8'


def mysql_get_connect():
    return pymysql.connect(host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PASSWORD, port=MYSQL_PORT, db=MYSQL_DB,
                           charset=MYSQL_CHARSET)


def select_course(cursor, data):
    sql = "select * from web_course where course_name = %s and course_teacher_name = %s "
    cursor.execute(sql, (data['course_title'], data['course_teacher_name']))
    if cursor.rowcount:
        return True
    else:
        return False


def insert_course(cursor, data):
    sql = "insert into mooc_course (course_name, course_img_url, course_teacher_name, course_price, course_source_id, created_at) value (%s, %s, %s, %s, %s, %s)"
    cursor.execute(sql,
                   (data['course_title'], data['course_img_url'], data['course_teacher_name'], data['course_price'], 1,
                    time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))


def save_course(cursor, data):
    if not select_course(cursor, data):
        insert_course(cursor, data)


def main():
    mysql_con = mysql_get_connect()
    cursor = mysql_con.cursor()
    mysql_con.autocommit(True)

    files = os.listdir('data/')
    for file in files:
        with open('./data/' + file) as f:
            data = f.readline()
            while data:
                save_course(cursor, json.loads(data))
                data = f.readline()


if __name__ == '__main__':
    main()
